﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WPF各种控件样式
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
            textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
        }

       

        void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox txt = sender as TextBox;

            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
            {
                if (txt.Text.Contains(".") && e.Key == Key.Decimal)
                {
                    e.Handled = true;
                    return;
                }
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                if (!txt.Text.Contains(".") && e.Key == Key.OemPeriod)
                {
                    e.Handled = true;
                    return;
                }
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }


        }
        void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //屏蔽中文输入和非法字符粘贴输入
            TextBox textBox = sender as TextBox;
            TextChange[] change = new TextChange[e.Changes.Count];
            e.Changes.CopyTo(change, 0);

            int offset = change[0].Offset;
            if (change[0].AddedLength > 0)
            {
                double num = 0;
                if (!Double.TryParse(textBox.Text, out num))
                {
                    textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
                    textBox.Select(offset, 0);
                }
            }

        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //WPFTabControl wpfTC = new WPFTabControl();
            //wpfTC.Owner = this;

            //wpfTC.Show();
         
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            WPFListViewCtrl win = new WPFListViewCtrl();
            win.Owner = this;
            win.Show();
        }
        string ss = "";
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //ChinaDate.ChinaDateText = "20123-2-4";
            bool b = ss.Equals(textBox1.Text.ToString());
            if (b)//一样
            {

            }
        }
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            bool b = ss.Equals(textBox1.Text.ToString());
            if (!b)
            {
                MessageBox.Show("关闭前保存");
            }

        }
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            WPFTreeViewCtrl wpftree = new WPFTreeViewCtrl();
            wpftree.Owner = this;
            wpftree.Show();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            lineTree win = new lineTree();
            win.Show();
        }

        private void SortClick(object sender, RoutedEventArgs e)
        {

        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void ChinaDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
         
            //DateTime? date =ChinaDate .SelectedDate;
            //string dateStr = date.Value.Year.ToString() + "年" + date.Value.Month.ToString() + "月" + date.Value.Day.ToString() + "日";
            //ChinaDate.Text = dateStr;
            ChinaDate.Text = (DateTime.Parse(ChinaDate.Text)).ToString("yyyy-MM-dd");
           
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            DataTimeWin dtw = new DataTimeWin();
            dtw.Show();
        }


    }
}
